package edu.sysu.cshare.model;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

@Entity
@Table(name = "T_GROUP")
public class Group implements Serializable {

	private long id;
	private String name;
	private Course course;
	private User leader;
	private List<User> members;
	private List<SubmitInfo> submitInfos;

	@Id
	@GeneratedValue
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	@Column(length = 255)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@ManyToOne
	public Course getCourse() {
		return course;
	}

	public void setCourse(Course course) {
		this.course = course;
	}

	@OneToOne
	public User getLeader() {
		return leader;
	}

	public void setLeader(User leader) {
		this.leader = leader;
	}

	@ManyToMany(targetEntity = User.class, fetch = FetchType.EAGER)
	@JoinTable(name = "TR_GROUP_USER", joinColumns = @JoinColumn(name = "group_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"))
	@Fetch(FetchMode.SUBSELECT)
	public List<User> getMembers() {
		return members;
	}

	public void setMembers(List<User> members) {
		this.members = members;
	}

	@OneToMany(mappedBy = "group", fetch = FetchType.EAGER)
	@Fetch(FetchMode.SUBSELECT)
	public List<SubmitInfo> getSubmitInfos() {
		return submitInfos;
	}

	public void setSubmitInfos(List<SubmitInfo> submitInfos) {
		this.submitInfos = submitInfos;
	}

}
